package cn.ctodb.mall.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.mall.service.MemberAddressService;
import cn.ctodb.mall.web.rest.util.HeaderUtil;
import cn.ctodb.mall.service.dto.MemberAddressDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing MemberAddress.
 */
@RestController
@RequestMapping("/api")
public class MemberAddressResource {

    private final Logger log = LoggerFactory.getLogger(MemberAddressResource.class);
        
    @Inject
    private MemberAddressService memberAddressService;

    /**
     * POST  /member-addresses : Create a new memberAddress.
     *
     * @param memberAddressDTO the memberAddressDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new memberAddressDTO, or with status 400 (Bad Request) if the memberAddress has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/member-addresses")
    @Timed
    public ResponseEntity<MemberAddressDTO> createMemberAddress(@RequestBody MemberAddressDTO memberAddressDTO) throws URISyntaxException {
        log.debug("REST request to save MemberAddress : {}", memberAddressDTO);
        if (memberAddressDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("memberAddress", "idexists", "A new memberAddress cannot already have an ID")).body(null);
        }
        MemberAddressDTO result = memberAddressService.save(memberAddressDTO);
        return ResponseEntity.created(new URI("/api/member-addresses/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("memberAddress", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /member-addresses : Updates an existing memberAddress.
     *
     * @param memberAddressDTO the memberAddressDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated memberAddressDTO,
     * or with status 400 (Bad Request) if the memberAddressDTO is not valid,
     * or with status 500 (Internal Server Error) if the memberAddressDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/member-addresses")
    @Timed
    public ResponseEntity<MemberAddressDTO> updateMemberAddress(@RequestBody MemberAddressDTO memberAddressDTO) throws URISyntaxException {
        log.debug("REST request to update MemberAddress : {}", memberAddressDTO);
        if (memberAddressDTO.getId() == null) {
            return createMemberAddress(memberAddressDTO);
        }
        MemberAddressDTO result = memberAddressService.save(memberAddressDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("memberAddress", memberAddressDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /member-addresses : get all the memberAddresses.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of memberAddresses in body
     */
    @GetMapping("/member-addresses")
    @Timed
    public List<MemberAddressDTO> getAllMemberAddresses() {
        log.debug("REST request to get all MemberAddresses");
        return memberAddressService.findAll();
    }

    /**
     * GET  /member-addresses/:id : get the "id" memberAddress.
     *
     * @param id the id of the memberAddressDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the memberAddressDTO, or with status 404 (Not Found)
     */
    @GetMapping("/member-addresses/{id}")
    @Timed
    public ResponseEntity<MemberAddressDTO> getMemberAddress(@PathVariable Long id) {
        log.debug("REST request to get MemberAddress : {}", id);
        MemberAddressDTO memberAddressDTO = memberAddressService.findOne(id);
        return Optional.ofNullable(memberAddressDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /member-addresses/:id : delete the "id" memberAddress.
     *
     * @param id the id of the memberAddressDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/member-addresses/{id}")
    @Timed
    public ResponseEntity<Void> deleteMemberAddress(@PathVariable Long id) {
        log.debug("REST request to delete MemberAddress : {}", id);
        memberAddressService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("memberAddress", id.toString())).build();
    }

    /**
     * SEARCH  /_search/member-addresses?query=:query : search for the memberAddress corresponding
     * to the query.
     *
     * @param query the query of the memberAddress search 
     * @return the result of the search
     */
    @GetMapping("/_search/member-addresses")
    @Timed
    public List<MemberAddressDTO> searchMemberAddresses(@RequestParam String query) {
        log.debug("REST request to search MemberAddresses for query {}", query);
        return memberAddressService.search(query);
    }


}
